﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace onHaj.Code.BusinessLogic
{

    public class Itinerary
    {
        public List<ItineraryLeg> Legs;

        public int NumberOgLegs =0;

        public bool IsReturnItinerary = true;

        public string ResponseLink { get; set; }

        public Itinerary(List<ItineraryLeg> legs )
        {

            Legs = legs;
            foreach (var itineraryLeg in legs.Where(itineraryLeg => !string.IsNullOrEmpty(itineraryLeg.From)))
            {
                NumberOgLegs++;

            }
            if (NumberOgLegs > 2) IsReturnItinerary = false;

            if (legs[0].To != legs[1].From) IsReturnItinerary = false;
        }

        public string PasteItineraryIntoString(string input)
        {
            StringBuilder sb = new StringBuilder(input);

            foreach (var itineraryLeg in Legs.Where(itineraryLeg => !string.IsNullOrEmpty(itineraryLeg.From)))
            {
                var legDictionary = itineraryLeg.ToDictionary();
                foreach (var legDetail in legDictionary)
                {
                    sb.Replace(legDetail.Key, legDetail.Value);
                }
            }
            return sb.ToString();
        }

    }
    public class ItineraryLeg
    {
        public int Sequence { get; set; }
        public string From
        {
            get { return DepartureAirport; }
        }

     
        public string To
        {
            get { return ArrivalAirport; }
        }

        public DateTime? RequestedDepartureDate { get; set; }
        public DateTime? RequestedArrivalDate { get; set; }
        public string DepartureAirport { get; set; }
        public string ArrivalAirport { get; set; }

        public string ETicketNumber { get; set; }
        public string FlightNumber { get; set; }
        
        public override string ToString()
        {
            if (string.IsNullOrEmpty(DepartureAirport) && string.IsNullOrEmpty(ArrivalAirport)) return string.Empty;

            string itinerary = DepartureAirport + "/" + ArrivalAirport + "       ";
            itinerary = itinerary.TrimEnd('/');
            if (RequestedDepartureDate.HasValue)
            {
                itinerary += "Departure " + RequestedDepartureDate.Value.ToLongDateString() + "-";
            }

            if (RequestedArrivalDate.HasValue)
            {
                itinerary += "Arrival " + RequestedArrivalDate.Value.ToLongDateString();
            }


            return itinerary.TrimEnd('-');
        }

        public Dictionary<string,string>  ToDictionary()
        {
            var dictionary = new Dictionary<string, string>
                                                       {
                                                           {"{From" + Sequence + "}", DepartureAirport},
                                                           {"{To" + Sequence + "}", ArrivalAirport}
                                                       };

            if (!string.IsNullOrEmpty(ArrivalAirport) && !string.IsNullOrEmpty(DepartureAirport))
                {
                    dictionary.Add("{DepartureDate" + Sequence + "}",
                                   RequestedDepartureDate.HasValue
                                       ? RequestedDepartureDate.Value.Date.ToLongDateString()
                                       : "Best Available Departure");

                    dictionary.Add("{ArrivalDate" + Sequence + "}",
                                   RequestedArrivalDate.HasValue
                                       ? RequestedArrivalDate.Value.Date.ToLongDateString()
                                       : "Best Available Arrival");
                }
                else
                {
                    dictionary.Add("{ArrivalDate" + Sequence + "}", "");
                    dictionary.Add("{DepartureDate" + Sequence + "}", "");
                }

            return dictionary;
        }
    }
}